<template>
  <el-container>
    <el-header>
      <div class="left-panel">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            @click="handleAdd"
        >新增
        </el-button>
        <el-button
            type="success"
            plain
            icon="el-icon-edit"
            :disabled="single"
            @click="handleUpdate"
        >修改
        </el-button>
        <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            :disabled="multiple"
            @click="handleDelete"
        >删除
        </el-button>
      </div>
      <div class="right-panel">
        <jcx-table-bar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></jcx-table-bar>
      </div>
    </el-header>
    <el-main>
      <el-form :model="queryParams" ref="queryForm" size="default" :inline="true" v-show="showSearch"
               label-width="68px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="配置名称" prop="configName">
              <el-input
                  v-model="queryParams.configName"
                  placeholder="请输入配置名称"
                  clearable
                  @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="配置状态" prop="status">
              <el-select v-model="queryParams.status" placeholder="请选择配置状态" clearable>
                <el-option key="1" label="启用" :value="true"/>
                <el-option key="0" label="停用" :value="false"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <el-table ref="table" v-loading="loading" :data="configList" row-key="configId"
                @selection-change="handleSelectionChange"
                :stripe="config.stripe" :border="config.border" :size="config.size" style="width: 100%">
        <el-table-column type="selection" width="50"></el-table-column>
        <el-table-column label="配置标识" align="center" prop="configCode" v-if="columns[0].visible"></el-table-column>
        <el-table-column label="配置名称" align="center" prop="configName" v-if="columns[1].visible"></el-table-column>
        <el-table-column label="配置类型" align="center" prop="configType" v-if="columns[2].visible">
          <template #default="props">
            <el-tag v-if="props.row.configType === 'PLATFORM_CONFIG'" type="primary">平台配置</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="参数键名" align="center" prop="configKey" v-if="columns[3].visible"></el-table-column>
        <el-table-column label="参数键值" align="center" prop="configValue" v-if="columns[4].visible"></el-table-column>
        <el-table-column label="状态" align="center" prop="status" v-if="columns[5].visible">
          <template #default="props">
            <el-tag v-if="props.row.status" type="success">启用</el-tag>
            <el-tag v-if="!props.row.status" type="danger">停用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" v-if="columns[6].visible"></el-table-column>
        <el-table-column label="备注" align="center" prop="remark" v-if="columns[7].visible"></el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="props">
            <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(props.row)"
            >修改
            </el-button>
            <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(props.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <jcx-page
          v-show="total>0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      ></jcx-page>

      <el-dialog destroy-on-close append-to-body width="900px" :title="title" v-model="open"
                 :close-on-click-modal="false" @closed="cancel">
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="配置标识" prop="configCode">
                <el-input v-model="form.configCode" clearable placeholder="请输入配置标识"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="配置名称" prop="configName">
                <el-input v-model="form.configName" clearable placeholder="请输入配置名称"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="参数键名" prop="configKey">
                <el-input v-model="form.configKey" clearable placeholder="请输入参数键名"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="参数键值" prop="configValue">
                <el-input v-model="form.configValue" clearable placeholder="请输参数键值"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="是否有效" prop="status">
                <el-switch v-model="form.status" :active-value="true" :inactive-value="false"></el-switch>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="配置类型" prop="configType">
                <el-radio-group v-model="form.configType">
                  <el-radio-button label="PLATFORM_CONFIG">平台配置</el-radio-button>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input type="textarea" v-model="form.remark" clearable placeholder="请输入备注"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <template #footer>
          <el-button type="primary" :loading="saveing" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </template>
      </el-dialog>
    </el-main>
  </el-container>
</template>

<script>
module.exports = {
  name: "Platform",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 保存遮罩层
      saveing: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 租户列表数据
      configList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 分页总条数
      total: 0,
      // 查询参数
      queryParams: {
        configType: "PLATFORM_CONFIG",
        configName: undefined,
        status: undefined,
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        configCode: [
          {required: true, message: '请输入配置标识', trigger: 'blur'}
        ],
        configName: [
          {required: true, message: '请输入配置名称', trigger: 'blur'}
        ],
        configType: [
          {required: true, message: '请选择配置类型', trigger: 'blur'}
        ],
        configKey: [
          {required: true, message: '请输入参数键名', trigger: 'blur'}
        ],
        configValue: [
          {required: true, message: '请输入参数键值', trigger: 'blur'}
        ],
        status: [
          {required: true, message: '请选择是否启用', trigger: 'blur'}
        ]
      },
      // 表格配置
      config: {
        // 表格大小
        size: "default",
        // 表格边框
        border: true,
        // 表格斑马图纹
        stripe: true
      },
      columns: [
        {key: 0, label: `配置标识`, visible: true},
        {key: 1, label: `配置名称`, visible: true},
        {key: 2, label: `配置类型`, visible: true},
        {key: 3, label: `参数键名`, visible: true},
        {key: 4, label: `参数键值`, visible: true},
        {key: 5, label: `状态`, visible: true},
        {key: 6, label: `创建时间`, visible: false},
        {key: 7, label: `备注`, visible: true}
      ],
    };
  },
  methods: {
    /** 查询部门列表 */
    getList() {
      this.loading = true;
      this.$API.config.pageListConfig.get(this.queryParams).then(response => {
        if (response.status) {
          this.configList = response.data.records
          this.total = response.data.total
        } else {
          this.$message.warning(response.msg);
        }
        this.loading = false;
      })
    },
    // 取消按钮
    cancel() {
      this.saveing = false;
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        configId: "",
        configCode: "",
        configName: "",
        configType: "PLATFORM_CONFIG",
        configKey: "",
        configValue: "",
        userId: "",
        remark: "",
        status: true
      };
      this.$nextTick(() => {
        if (this.$refs['form']) {
          this.$refs['form'].clearValidate();
          this.$refs['form'].resetFields();
        }
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs["queryForm"].resetFields();
      this.handleQuery();
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.configId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const configId = row.configId || this.ids
      this.$API.config.getConfigById.get(configId).then(response => {
        if (response.status) {
          this.form = response.data;
          this.open = true;
          this.title = "修改配置";
        } else {
          this.$message.warning(response.msg);
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.saveing = true;
          // 如果 ID 存在就是更新
          if (this.form.configId) {
            this.$API.config.editConfig.put(this.form).then(response => {
              if (response.status) {
                this.$message.success(response.msg);
                this.saveing = false;
                this.open = false;
                this.getList();
              } else {
                this.$alert(response.msg, "提示", {type: 'error'});
                this.saveing = false;
              }
            });
          } else {
            // 不存在就是新增
            this.$API.config.addConfig.post(this.form).then(response => {
              if (response.status) {
                this.$message.success(response.msg);
                this.saveing = false;
                this.open = false;
                this.getList();
              } else {
                this.$alert(response.msg, "提示", {type: 'error'});
                this.saveing = false;
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const configIds = row.configId ? [row.configId] : this.ids;
      this.$confirm(`确定删除选中的 ${configIds.length} 项吗？`, '提示', {
        type: 'warning'
      }).then(() => {
        this.$API.config.delConfigs.delete(configIds).then(response => {
          if (response.status) {
            this.$message.success(response.msg);
            this.getList();
          } else {
            this.$alert(response.msg, "提示", {type: 'error'});
          }
        });
      }).catch(() => {
      })
    }
  }
};
</script>

<style scoped>

</style>
